<?php
//session_start(); // 开启session功能

/**
 * 项目调试小工具
 * @version 1.0 2017-9-20 Added.
 * @version 2.0 2017-12-19 重构, 把通用类和方法剥离到debug_head.php头文件
 */
// 引入头文件
include 'debug_head.php';
// 版本号
define('VERSION', '2.0.0');
// 程序名称
define('APPNAME', 'DEBUGTOOL');

$action = trim(input('action')); // 当前动作
$value = trim(input('value')); // 值
$type = trim(input('type')); // 类型

switch ($action) {
    case 'clearsmscoder':
        if ($type && $value) {
            $avalue = explode(',', $value);
            $sqlstmt = "delete from tbl_deal_msg_list where send_type = 0 and $type in ('" . implode("','", $avalue) . "');";
            $result = $dbutil->execute($sqlstmt);
            header('location:?token=' . $token);
        }
        break;
    case 'deleteuser':
        if ($type && $value) {
            $sqlstmt = "delete from tbl_user where $type = '$value'";
            $result = $dbutil->execute($sqlstmt);
            header('location:?token=' . $token);
        }
        break;
    case 'delguardians':
        if ($type && $value) {
            $sqlstmt = "delete from tbl_user_guardians where $type in ($value)";
        } else {
            $sqlstmt = "truncate table tbl_user_guardians";
        }
        $result = $dbutil->execute($sqlstmt);
        header('location:?token=' . $token);
        break;
}
?>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title><?php echo APPNAME . ' - ' . VERSION ?></title>
        <style type="text/css">
            body, td, th{font-size:13px; color:#333;}
            fieldset{ min-width:45%;}
            a{text-decoration: none;}
            h1 a { color:#555; font-weight: normal;}
            .box { margin-bottom: 5px; width:45%; max-height:350px; min-height:100px; float:left;}
            .grid_container { max-height: 300px; overflow: auto;}
            .grid { border:solid 1px #CCC; border-bottom:none; border-right: none;}
            .grid tr:hover{background-color: #efefef;}
            .grid tr.active{background-color: #ffc;}
            .grid th, .grid td { border:solid 1px #CCC; border-left:none; border-top:none; padding:3px; word-break:break-all; text-align: left;}
        </style>
    </head>
    <body>
        <?php
        echo '<h1>', APPNAME, ' ';
        echo '<a href="?token=', $token, '" title="新开一个页面" target="_blank" style="font-size:12px;">[新窗口]</a>';
        echo '<a href="?token=', $token, '" title="重置当前页面" style="font-size:12px;">[重置]</a>';
        echo '<a href="#" title="刷新当前页面" style="font-size:12px;" onclick="location.reload()">[刷新]</a>';
        echo '</h1>';
        // 用户登录信息
        if (isset($loginid)) {
            echo $loginid;
            echo ' [<a href="?action=logout" onclick="return confirm(\'您确定要注销登录吗?\');">注销登录</a>]';
            echo ' 当前IP ', $_SERVER['REMOTE_ADDR'];
        }
        ?>
        <div style="clear:both"></div>
        <div class="box">
            <fieldset>
                <legend>短信验证码<a href="#" class="lnk-switch">[-]</a></legend>
                <div class="fieldset_content">
                    <form action="?action=clearsmscoder">
                        <?php html_radio('type', array('client_ip' => 'IP', 'dest' => '手机号码'), ($action == 'clearsmscoder' ? $type : '')) ?>
                        ：<input type="text" name="value" size="90" placeholder="多个手机号码以英文逗号隔开" required="required" />
                        <input type="hidden" name="action" value="clearsmscoder" />
                        <input type="hidden" name="token" value="<?php echo $token ?>" />
                        <button type="submit">清空验证码</button>
                    </form>
                    <div class="grid_container">
                        <?php
                        // 显示最新手机验证码
                        $list = $dbutil->query('select `id`, `verify_code`, `mobile`, `create_time`, `client_ip` from tbl_mobile_verify_code order by `id` desc limit 10;');
                        if ($list) {
                            show_table($list, array('head' => array('id' => 'ID', 'verify_code' => '验证码', 'mobile' => '手机号', 'create_time' => '时间', 'client_ip' => 'IP'), 'fn' => array('create_time' => 'from_unixtime')));
                        }
                        ?>
                    </div>
                    <div>
                        </fieldset>
                    </div>
                    <div class="box">
                        <fieldset>
                            <legend>查看用户<a href="#" class="lnk-switch">[-]</a></legend>
                            <div class="fieldset_content">
                                <form action="?action=">
                                    类型：<?php html_radio('type', array('id' => '用户ID', 'nick_name' => '用户昵称', 'mobile' => '手机号'), ($action == 'selectuser' ? $type : '')) ?><br />
                                    <input type="text" name="value" placeholder="用户ID/用户昵称/手机号码,多个之间以英文逗号隔开" required="required" size="60" value="<?php echo 'selectuser' == $action ? $value : '' ?>" />
                                    <input type="hidden" name="action" value="selectuser" />
                                    <input type="hidden" name="token" value="<?php echo $token ?>" />
                                    <button type="submit">查看</button>
                                </form>
                                <?php
                                if ('selectuser' == $action && $value) {
                                    $sqlstmt = "select id, nick_name from tbl_user where $type in ('" . implode("','", explode(',', $value)) . "') order by id";
                                    $list = $dbutil->query($sqlstmt);
                                    if ($list) {
                                        $sqls = array(
                                            '用户表' => "select id, nick_name, mobile, coin, diamonds, ticket, wx_openid, wx_unionid, invite_id, bm_pid, bm_qrcode_id, bm_promoter_id from tbl_user where id=var_user_id",
                                            '用户日志表' => "select `id`, `log_info`, `log_time`, `user_id`, `money`, `diamonds`, `ticket`, `video_id`, `type`, `prop_id`, `podcast_id` from tbl_user_log where user_id=var_user_id order by `id` desc limit 5;",
                                            '用户金币日志表' => 'select `id`, `user_id`, `game_log_id`, `create_time`, `diamonds`, `account_diamonds`, `memo` from tbl_coin_log where user_id=var_user_id order by id desc limit 5;',
                                            '用户竞猜投注日志表' => 'select `id`, `guessing_log_id`, `user_id`, `xiacai_id`, `guessing_id`, `money`, `is_bet`, `status`, `create_time`, `create_date` from tbl_user_guessing_log where user_id=var_user_id order by id desc limit 5;',
                                            '用户游戏投注日志表' => 'select `id`, `game_log_id`, `user_id`, `money`, `bet`, `podcast_id`, `create_time`, `create_date`, `type` from tbl_user_game_log where user_id=var_user_id order by id desc limit 5;',
                                        );
                                        foreach ($list as $row) {
                                            $user_id = $row['id'];
                                            $nick_name = $row['nick_name'];
                                            echo '<fieldset><legend>用户[', $user_id, ']</legend><div>';
                                            foreach ($sqls as $title => $sqlstmt) {
                                                $sqlstmt = str_replace('var_user_id', $user_id, $sqlstmt);
                                                $list = $dbutil->query($sqlstmt);
                                                echo '<strong>', $title, '</strong><br />';
                                                show_table($list, array('fn' => array('create_time' => 'from_unixtime')));
                                            }
                                            echo '</fieldset>';
                                        }
                                    } else {
                                        echo '<div>无记录</div>';
                                    }
                                }
                                ?>
                            </div>
                        </fieldset>
                    </div>
                    <div class="box">
                        <fieldset>
                            <legend>删除用户<a href="#" class="lnk-switch">[-]</a></legend>
                            <div class="fieldset_content">
                                <form action="?action="  onsubmit="return confirm('您确定要删除用户吗?')">
                                    类型：<?php html_radio('type', array('id' => '用户ID', 'nick_name' => '用户昵称', 'mobile' => '手机号'), ($action == 'deleteuser' ? $type : '')) ?><br />
                                    <input type="text" name="value" placeholder="用户ID/用户昵称/手机号码" required="required" size="60" />
                                    <input type="hidden" name="action" value="deleteuser" />
                                    <input type="hidden" name="token" value="<?php echo $token ?>" />
                                    <button type="submit">删除</button>
                                </form>
                            </div>
                        </fieldset>
                    </div>
                    <div class="box">
                        <fieldset>
                            <legend>用户推广<a href="#" class="lnk-switch">[-]</a></legend>
                            <div class="fieldset_content">
                                <?php
                                $invite_id = $value; // 推广主播ID
                                $type = input('type');
                                ?>
                                <form action="?action=">
                                    类型：<?php html_radio('type', array('anchor_id' => '推广主播ID', 'p_login_name' => '所属二级单位ID'), ($action == 'promoter' ? $type : '')) ?><br />
                                    <input type="text" name="value" placeholder="推广主播ID/所属二级单位ID" required="required" value="<?php echo 'promoter' == $action ? $value : '' ?>" size="60" />
                                    <input type="hidden" name="action" value="promoter" />
                                    <input type="hidden" name="token" value="<?php echo $token ?>" />
                                    <button type="submit">查询</button>
                                </form>
                                <?php
                                // 显示用户推广会员
                                if ($action == 'promoter' && $invite_id) {
                                    // 推广员信息
                                    echo '<div>推广员信息</div>';
                                    $data = $dbutil->find("select id as 主播ID, nick_name as 主播名称, is_authentication as 是否认证, ticket as 星值, bm_pid as 所属二级单位ID, bm_promoter_id as 业务员ID, invite_id as 推广主播ID from tbl_user where id=$invite_id");
                                    if (!$data) {
                                        echo '<div>推广员：', $invite_id, '不存在！</div>';
                                    } else {
                                        show_table($data, array('is_list' => false));
                                    }
                                    echo '<div>推广用户列表</div>';
                                    // 推广列表
                                    $field = $type == 'anchor_id' ? 'invite_id' : 'bm_pid';
                                    $list = $dbutil->query('select id as 主播ID, nick_name as 主播名称, is_authentication as 是否认证, ticket as 星值, bm_pid as 所属二级单位ID, bm_promoter_id as 业务员ID, invite_id as 推广主播ID from tbl_user where ' . $field . '=' . $invite_id . ' order by `id` desc;');
                                    if ($list) {
                                        show_table($list);
                                    }
                                }
                                ?>
                            </div>
                        </fieldset>
                    </div>
                    <div class="box">
                        <fieldset>
                            <legend>清空用户守护记录<a href="#" class="lnk-switch">[-]</a></legend>
                            <div class="fieldset_content">
                                <?php
                                $type = input('type');
                                ?>
                                <form action="?action=">
                                    类型：<?php html_radio('type', array('to_user_id' => '主播ID', 'from_user_id' => '用户ID'), ($action == 'guardians' ? $type : '')) ?><br />
                                    <input type="text" name="value" placeholder="主播ID/用户ID" required="required" value="<?php echo 'guardians' == $action ? $value : '' ?>" size="60" />
                                    <input type="hidden" name="action" value="guardians" />
                                    <input type="hidden" name="token" value="<?php echo $token ?>" />
                                    <button type="submit">查询</button>
                                    <button type="button" onclick="return submitform(this, 'delguardians')">清空</button>
                                </form>
                                <?php
                                $value = intval($value);
                                if ($action == 'guardians' && $value) {
                                    // 推广员信息
                                    $list = $dbutil->query("select to_user_id as 主播ID, from_user_id as 用户ID, prop_name as 守护名称, from_unixtime(end_time+28800) as 过期时间 from tbl_user_guardians where $type=$value and is_effect=1");
                                    show_table($list);
                                }
                                ?>
                            </div>
                        </fieldset>
                    </div>
                    <script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
                    <script>
                                        // 展开或收起fieldset内容
                                        $('legend .lnk-switch').click(function () {
                                            var 　$a = $(this);
                                            if ($a.text() == '[-]') {
                                                $a.text('[+]').attr('title', '展开');
                                                $a.parent().next().hide();
                                            } else {
                                                $a.text('[-]').attr('title', '收起');
                                                $a.parent().next().show();
                                            }
                                        })
                                        function submitform(btn, action) {
                                            var $form = $(btn).parent();
                                            $form.find('input[name="action"]').val(action);
                                            $form.submit();

                                        }
                    </script>  
                    </body>
                    </html>